<?php
/**
 * 首页幻灯片类
 * author: heimuya
 * time: 2014-4-15
 */
 namespace Admin\Controller;
 use Think\Controller;
 
class FlashController extends CommonController {
	
	public function _initialize(){
		parent::_initialize();
	}
	
	public function flashlist(){
		$flash = M('Flash');
		$count = $flash->count();
		$Page = new \Think\Page($count, 10);
		$flashlist = $flash
					->limit($Page->firstRow. ',' . $Page->listRows)
					->order('ctime desc')
					->select();
		$this->empty = "<td colspan='7' class='tb_heade'>暂无数据</td>";
		$page = $Page->show();
		
		$this->assign("page", $page);
		$this->assign('flashlist',$flashlist);
		$this->display();
	}
	
	public function addflash(){
		$this->display();
	}
	
	public function doaddflash(){
		$upload = new \Think\Upload();// ;// 实例化上传类
		$upload->maxSize = 1024000; // 设置附件上传大小 
        $upload->exts = array('jpg', 'gif', 'jpeg', 'png'); // 设置附件上传类型 
        $upload->autoSub = FALSE; // 设置附件上传类型
		$upload->savePath = './static/'; // 设置附件上传目录 
		$info = $upload->upload();
		if(!$info) {
			$this->error('上传失败，可能的原因是您没有选择文件,或文件大于500kb');
		}else{
			$data['image'] = __ROOT__.'/Uploads/static/'.$info['image']['savename'];
			$data['brief'] = I('brief');
			$data['url'] = I('url');
			$data['sequence'] = $this->checkSequence('Flash', I('post.sequence'));
			$data['visible'] = I('ifshow');
			$data['ctime'] = time();
			if($data['brief'] == '' || $data['url'] == '' || $data['image'] == ''){
				print_r($data);
				die;
				$this->error('内容、url、和幻灯片必须，不能为空，URL没有请使用#代替');
			}
			$flash = M('Flash');
			if($rs = $flash->add($data)){
				oplog('增加幻灯片图片',$_SESSION['auser'],1);
				$this->success('增加成功');
			}else{
				$this->error('增加失败');
			}
		}
	}
	
	public function editflash(){
		$fid = I('fid');
		$flash = M('Flash');
		$w['id'] = $fid; //构造查询条件
		$f = $flash->where($w)->find();
		$this->assign('fdata',$f);
		$this->display();
	}
	
	public function doeditflash(){
		$upload = new \Think\Upload();// ;// 实例化上传类
		$upload->maxSize = 512000 ; // 设置附件上传大小 
        $upload->exts = array('jpg', 'gif', 'jpeg', 'png'); // 设置附件上传类型 
        $upload->autoSub = FALSE; // 设置附件上传类型
		$upload->savePath = './static/'; // 设置附件上传目录 
		$info = $upload->upload();
		if($info) {
			$data['image'] = __ROOT__.'/Uploads/static/'.$info['image']['savename'];
		}
		$data['brief'] = I('brief');
		$data['url'] = I('url');
		$data['sequence'] = $this->checkSequence('Flash', I('post.sequence'), 'edit', I('id'));
		$data['visible'] = I('ifshow');
		if($data['brief'] == '' || $data['url'] == ''){
			$this->error('内容、url、和幻灯片必须，不能为空，URL没有请使用#代替');
		}
		$rs = M('Flash')->where(array('id'=>I('id')))->setField($data);
		if($rs !== false){
			oplog('编辑幻灯图片',$_SESSION['auser'],1);
			$this->success('编辑成功', U('Flash/flashlist'));
		}else{
			$this->error('编辑失败');
		}
	}
	
	public function delflash(){
		$fid = I('fid'); 
		if($fid != ''){
			$fimg = M('Flash')->field('image')->where(array('id'=>$fid))->find();
			$rs = M('Flash')->where(array('id'=>$fid))->delete();
			if($rs !== false){
				@unlink($fimg['image']);//删除幻灯图片
				oplog('删除幻灯图片',$_SESSION['auser'],1);
				$this->success('删除成功');
			}else {
				$this->error('删除失败，稍后重试，或者联系技术部门处理');
			}
		}else {
			$this->error('参数错误');
		} 
	}
}